package com.donggua.aichebao.commonui.load;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.donggua.aichebao.commonui.R;

/**
 * Created by Administrator on 2017-07-14.
 */

public class LoadInputLayout extends RelativeLayout implements View.OnClickListener {
    private EditText etInput;
    private Button  btnRight;
    private Drawable btnDrawable;
    private int inputStyle = TEXT;
    public final static int TEXT = 0;
    public final static int PASSOWRD = 1;
    public final static int CHECK = 2;
    public final static int NUMBER = 3;
    private static final int BTNId = 1000;
    private static final int LEFTID = 1001;
    private boolean isShowPassword = false;
    private int defaultSize;
    private int margin;
    public LoadInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CustomLoad);
        defaultSize = (int)context.getResources().getDimension(R.dimen.load_btn_default_size);
         margin = (int)context.getResources().getDimension(R.dimen.margin);
        inputStyle =  ta.getInt(R.styleable.CustomLoad_inputStyle,TEXT);
        if(inputStyle != NUMBER){
            setRightBtn(context,ta);
        }
        setRightImage(context,ta);
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        etInput = new EditText(context);
        LayoutParams etParams = new LayoutParams
                (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        if(inputStyle != NUMBER){
            etParams.addRule(RelativeLayout.LEFT_OF,BTNId);
        }
        etParams.addRule(RelativeLayout.RIGHT_OF,LEFTID);
        etParams.addRule(verticalCenterRule,RelativeLayout.TRUE);
        String hint =  ta.getString(R.styleable.CustomLoad_hintText);
        Log.e("hit"," hint is "+hint);
        if(hint != null){
            etInput.setHint(hint);
        }
        etInput.setBackgroundDrawable(null);
        etInput.setSingleLine(true);
        etInput.setPadding(margin*2,0,0,0);
        etInput.setLayoutParams(etParams);
        /*
        Drawable leftDrawable = ta.getDrawable(R.styleable.CustomLoad_leftDrawable);
        if(leftDrawable!=null){
            leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
            etInput.setCompoundDrawables(leftDrawable,null,null, null);
           // etInput.setCompoundDrawablePadding(leftDrawablePadding);
        }*/
        Log.e("hit"," inputStyle is "+inputStyle);
        this.addView(etInput);
        this.setBackgroundResource(R.drawable.load_input_bk);
        setInputStyle();
        ta.recycle();
    }

    private void setRightImage(Context context, TypedArray ta) {
        ImageView leftImg = new ImageView(context);
        LayoutParams btnParams = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        leftImg.setId(LEFTID);
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        int leftRule = RelativeLayout.ALIGN_PARENT_LEFT;
        btnParams.setMargins(0,0,margin,0);
        btnParams.addRule(leftRule,RelativeLayout.TRUE);
        btnParams.addRule(verticalCenterRule ,RelativeLayout.TRUE);
        Drawable leftDrawable = ta.getDrawable(R.styleable.CustomLoad_leftDrawable);
        if(leftDrawable != null){
            leftImg.setImageDrawable(leftDrawable);
        }
        leftImg.setLayoutParams(btnParams);
        this.addView(leftImg);
    }

    private void setRightBtn(Context context,  TypedArray ta) {
        btnRight = new Button(context);
        LayoutParams btnParams = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        btnRight.setId(BTNId);
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        int rightRule = RelativeLayout.ALIGN_PARENT_RIGHT;
        btnParams.addRule(rightRule,RelativeLayout.TRUE);
        btnParams.addRule(verticalCenterRule ,RelativeLayout.TRUE);
        btnDrawable = ta.getDrawable(R.styleable.CustomLoad_btnDrawable);
        if(btnDrawable != null){
            btnRight.setBackgroundDrawable(btnDrawable);
        }
        int btnWidth = (int)ta.getDimension(R.styleable.CustomLoad_btnWidth,defaultSize);
        int btnHeight = (int)ta.getDimension(R.styleable.CustomLoad_btnHeight,defaultSize);
        btnParams.width = btnWidth;
        btnParams.height = btnHeight;
        btnParams.leftMargin = margin;
        btnRight.setLayoutParams(btnParams);
        String text = ta.getString(R.styleable.CustomLoad_btnText);
        Log.e("hit"," text is "+text);
        if(text != null){
            btnRight.setText(text);
        }
        this.addView(btnRight);
        btnRight.setOnClickListener(this);
    }

    private void setInputStyle(){
        switch (inputStyle){
            case TEXT:
                etInput.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
            case PASSOWRD:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case CHECK:
                etInput.setInputType(InputType.TYPE_CLASS_PHONE);
                break;
            case NUMBER:
                etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
        }
    }
    public LoadInputLayout(Context context) {
        super(context);
    }

    public void clearInputText(){
        etInput.setText("");
    }

    @Override
    public void onClick(View view) {
        switch (inputStyle){
            case TEXT:
                clearInputText();
                break;
            case PASSOWRD:
                if(!isShowPassword){
                    isShowPassword = true;
                    etInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }else{
                    isShowPassword = false;
                    etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;
            case CHECK:
                break;
        }
    }

    public String getInputText(){
        if(etInput != null){
            return etInput.getText().toString();
        }
        return "";
    }
}
